#ifndef _STATE_HPP_
#define _STATE_HPP_

#include <string>

extern "C" {
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
}

#include "global.hpp"
#include "lua.hpp"

#define TERMINAL_MAX_LINE_SIZE 1024
#define CONFIG_FILENAME ".client_config.lua"
#define PATH_DELIM '/'

class State {
public:	

	State(verbosity v, const char *config);
	
	int run();
	int m(verbosity verb, const char *fmt, ... );
	void PrintHelp();
	
	inline int sock() {
		return sockfd;
	}
	inline const struct sockaddr_in* addr() {
		return &dest_addr;
	}
	inline unsigned short port() {
		return server_port;
	}
	
	static void *InputHandler( void *data );
	
	bool initialized, done;
	
	void PrintServerMessage(response_types type, client_actions action, server_error error);
	
protected:
	unsigned short server_port;
	int sockfd;
	struct sockaddr_in dest_addr;
	std::string config_name, server_address, username, password;
	verbosity log_verbosity;
	pthread_t readline_thread;
};

#endif //_STATE_HPP_